<!DOCTYPE html>
<html>
<head lang="zh-cmn-Hans">
    <meta charset="UTF-8">
    <title>Ajax hook Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=0.5,user-scalable=no"/>
    <meta name="keywords" content="Ajax hook Demo">
    <script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
    <script src="../dist/ajaxhook.min.js"></script>

</head>
<style>
    html {
        font-size: 20px;
        -webkit-user-select: none;
    }
</style>
<body>
<div style="background: #000; font-size: 38px; color: #ffef68; text-shadow: 2px 2px 10px #ffef68; width: 400px; height: 400px; text-align: center">
    <div style="padding-top: 100px"> Ajax Hook !</div>
</div>
open console panel to view log.
<script>

    hookAjax(
        // hook functions and callbacks of XMLHttpRequest object
        {
            onreadystatechange: function (xhr) {
                console.log("onreadystatechange called: %O", xhr)
                //return true
            },
            onload: function (xhr) {
                console.log("onload called: %O", xhr)
                xhr.responseText = "hook" + xhr.responseText;
                //return true;
            },
            open: function (arg, xhr) {
                console.log("open called: method:%s,url:%s,async:%s", arg[0], arg[1], arg[2], xhr)
                arg[1] += "?hook_tag=1";
                //统一添加请求头

            },
            send: function (arg, xhr) {
                console.log("send called: %O", arg[0])
                xhr.setRequestHeader("_custom_header_", "ajaxhook")
            },
            setRequestHeader: function (arg, xhr) {
                console.log("setRequestHeader called!", arg)
            },

            // hook attributes of XMLHttpRequest object
            timeout: {
                setter: function (v, xhr) {
                    //timeout shouldn't exceed 10s
                    return Math.max(v, 1000);
                }
            }
        }
    );

    $.get().done(function (d) {
        console.log(d.substr(0, 30) + "...")
        //use original XMLHttpRequest
        console.log("unhook")
        unHookAjax()
        $.get().done(function (d) {
            console.log(d.substr(0, 10))
        })

    })

</script>
</body>
</html>